package com.ycsoft.ttctn.pkgmember;

import java.util.Collections;

/**
 * Created by ChenHui on 2018/3/21/021.
 */

// 定义包格式的描述类。
class FormatIdDesc {
    public String c;
    public String s;
    public String prec;

    public FormatIdDesc() {
        c = "";
        s = "";
        prec = "";
    }

    public int getSize() {
        if (s.isEmpty())
            return 0;
        else {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                return 0;
            }
        }
    }

    public int getPrec() {
        if (s.isEmpty())
            return 0;
        else {
            try {
                return Integer.parseInt(prec);
            } catch (NumberFormatException e) {
                return 0;
            }
        }
    }
}

// 创建数据包格式化的基础类。
public class MemFormat {
    public static FormatIdDesc decodeFormatId(String formatId) {
        FormatIdDesc desc = new FormatIdDesc();
        desc.c = formatId.substring(1, 1);
        desc.s = formatId.substring(2, 4);
        desc.prec = formatId.substring(6, 11);

        return desc;
    }

    public static int memberSize(String formatId) {
        FormatIdDesc desc = decodeFormatId(formatId);

        return desc.getSize();
    }

    public static String getFormatMember(String formatId, Object value, boolean disp, char fillChar) {
        String result = "", value_str;
        FormatIdDesc desc = decodeFormatId(formatId);
        int size = desc.getSize();

        switch (desc.c.toUpperCase()) {
            case "C":  //字符串类型，前填字符达到指定的长度。
                StringBuffer sb = new StringBuffer();

                value_str = value.toString();
                for(int i = 1; i<= size - value_str.length(); i++)
                    sb = sb.append(fillChar);
                sb = sb.append(value_str);

                result =  sb.toString();
                break;
            case "X":
            case "N":
                int prec = desc.getPrec();
                /*
                long l;

                if (value instanceof Long) {
                    l = Long.parseLong(value.toString());
                } else if (value instanceof  Integer) {
                    l = Integer.parseInt(value.toString());
                }

                if (prec>0) {


                } else {

                }
                */

                break;
        }



        return result;
    }



}
